package ch.manage.movie.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class MovieInfo extends JPanel {

	private static final long serialVersionUID = 1L;
	private JTextField movieTitle;
	private JTextField movieGenre;
	private JTextArea movieDescription;
	private JTextField movieReleased;
	private MovieCover movieCover;
	private JTextField movieRated;
	private JTextField movieDuration;
	private JTextField movieDirectors;
	
	public MovieInfo(){
		setMinimumSize(new Dimension(200, 600));
		setLayout(new BorderLayout(0, 0));
		
		JSplitPane splitPane = new JSplitPane();
		add(splitPane, BorderLayout.CENTER);
		splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
		
		
		JLabel movieInfoLabel = new JLabel("MOVIE INFO");
		add(movieInfoLabel, BorderLayout.NORTH);
		
		movieCover = new MovieCover();
		movieCover.setMinimumSize(new Dimension(50, 50));
		movieCover.setPreferredSize(new Dimension(100, 250));
		splitPane.add(movieCover,JSplitPane.TOP);
		
		JPanel outerPanel = new JPanel();
		outerPanel.setPreferredSize(new Dimension(200, 10));
		outerPanel.setLayout(new GridLayout(10, 1, 0, 0));
		
		JScrollPane scrollPane = new JScrollPane(outerPanel);
		splitPane.add(scrollPane, JSplitPane.BOTTOM);
		
		JLabel movieTitleLabel = new JLabel("Title");
		outerPanel.add(movieTitleLabel);
		
		movieTitle = new JTextField();
		movieTitleLabel.setLabelFor(movieTitle);
		outerPanel.add(movieTitle);
		movieTitle.setColumns(10);
		
		JLabel genreLabel = new JLabel("Genre");
		outerPanel.add(genreLabel);
		
		movieGenre = new JTextField();
		genreLabel.setLabelFor(movieGenre);
		outerPanel.add(movieGenre);
		movieGenre.setColumns(10);
		
		JLabel descriptionLabel = new JLabel("Description");
		outerPanel.add(descriptionLabel);
		
		movieDescription = new JTextArea();
		movieDescription.setLineWrap(true);
		descriptionLabel.setLabelFor(movieDescription);
		outerPanel.add(movieDescription);
		
		JLabel releasedLabel = new JLabel("Release Date");
		outerPanel.add(releasedLabel);
		
		movieReleased = new JTextField();
		releasedLabel.setLabelFor(movieReleased);
		outerPanel.add(movieReleased);
		movieReleased.setColumns(10);
		
		JLabel ratedLabel = new JLabel("Rated");
		outerPanel.add(ratedLabel);
		
		movieRated = new JTextField();
		ratedLabel.setLabelFor(movieRated);
		outerPanel.add(movieRated);
		movieRated.setColumns(10);
		
		JLabel durationLabel = new JLabel("Duration");
		outerPanel.add(durationLabel);
		
		movieDuration = new JTextField();
		outerPanel.add(movieDuration);
		movieDuration.setColumns(10);
		
		JLabel directorLabel = new JLabel("Directors");
		outerPanel.add(directorLabel);
		
		movieDirectors = new JTextField();
		outerPanel.add(movieDirectors);
		movieDirectors.setColumns(10);
	}

	public void setMovieTitle(String movieTitle) {
		this.movieTitle.setText(movieTitle);
	}

	public void setMovieGenre(String movieGenre) {
		this.movieGenre.setText(movieGenre);
	}

	public void setMovieDescription(String movieDescription) {
		this.movieDescription.setText(movieDescription);
	}
	
	public void setMovieCover(BufferedImage movieCoverImage){
		this.movieCover.setMovieCover(movieCoverImage);
	}
	
	public void setMovieReleased(String movieReleased){
		this.movieReleased.setText(movieReleased);
	}
	
	public class MovieCover extends JPanel{
		private static final long serialVersionUID = 1L;
		private BufferedImage originalCoverImage;
	    private BufferedImage clonedCoverImage;
	    private double scaleFactor;
	    private double scaleXFactor;
	    private double scaleYFactor;
	    private AffineTransform tx;
	    private AffineTransformOp op;

	    public void setMovieCover(BufferedImage movieCoverImage){
	    	
			this.originalCoverImage = movieCoverImage;
			if(originalCoverImage != null){
				clonedCoverImage = new BufferedImage(originalCoverImage.getWidth(), originalCoverImage.getHeight(), BufferedImage.TYPE_INT_RGB);
			}
		}
	    
	    @Override
	    public void paint(Graphics g) {
	    	if(originalCoverImage != null){
	    		this.clonedCoverImage = originalCoverImage;
	    		scaleXFactor = (double)this.getWidth()/originalCoverImage.getWidth();
	    		scaleYFactor = (double)this.getHeight()/originalCoverImage.getHeight();
	    		if(scaleXFactor<scaleYFactor){
	    			scaleFactor = scaleXFactor;
	    		}else{
	    			scaleFactor = scaleYFactor;
	    		}
				tx = new AffineTransform();
				tx.scale(scaleFactor, scaleFactor);
				op = new AffineTransformOp(tx,AffineTransformOp.TYPE_BILINEAR);
				clonedCoverImage = op.filter(clonedCoverImage, null);
	    		g.drawImage(clonedCoverImage, 0, 0, null);	    		
	    	}
	    }

	}
}
